﻿SET QUOTED_IDENTIFIER ON
GO
SET ARITHABORT ON
GO
SET NUMERIC_ROUNDABORT OFF
GO
SET CONCAT_NULL_YIELDS_NULL ON
GO
SET ANSI_NULLS ON
GO
SET ANSI_PADDING ON
GO
SET ANSI_WARNINGS ON
GO
-- =============================================
-- Author:		Adrian Kaczmarek
-- Create date: 2013-08-05
-- Description:	Konwertuje date do int'a
-- =============================================
CREATE FUNCTION bhf_ConvertDateTo 
(
	@Date datetime
)
RETURNS int
AS
BEGIN
	DECLARE @Result int

	SELECT @Result = DATEDIFF(d, '01-01-2001', @Date) + 73053

	RETURN @Result

END
GO
-- =============================================
-- Author:		Adrian Kaczmarek
-- Create date: 2013-08-05
-- Description:	Konwertuje wcześniej skonwertowaną date (int) do datetime
-- =============================================
CREATE FUNCTION [dbo].[bhf_ConvertDateFrom] 
(
	@DateConverted int
)
RETURNS datetime
AS
BEGIN
	DECLARE @Result datetime

	SELECT @Result = DATEADD(d, @DateConverted - 73053, '01-01-2001')

	RETURN @Result

END
GO
-- =============================================
-- Author:		Adrian Kaczmarek
-- Create date: 2013-08-05
-- Description:	Konwertuje date z czasem do int'a (unix)
-- =============================================
CREATE FUNCTION [dbo].[bhf_ConvertDateTimeTo] 
(
	@Date datetime
)
RETURNS bigint
AS
BEGIN
	DECLARE @Result bigint

	IF @Date >= '20380119' 
    BEGIN 
        SET @Result = CONVERT(BIGINT, DATEDIFF(S, '19700101', '20380119')) 
            + CONVERT(BIGINT, DATEDIFF(S, '20380119', @Date)) 
    END 
    ELSE 
        SET @Result = DATEDIFF(S, '19700101', @Date) 
    RETURN @Result 

END
GO
-- =============================================
-- Author:		Adrian Kaczmarek
-- Create date: 2013-08-05
-- Description:	Konwertuje wcześniej skonwertowaną date z czasem (int - unix) do datetime
-- =============================================
CREATE FUNCTION [dbo].[bhf_ConvertDateTimeFrom] 
(
	@UnixTimestamp bigint
)
RETURNS datetime
AS
BEGIN
	DECLARE @Result datetime

	SELECT @Result = DATEADD(second, @UnixTimestamp,{d '1970-01-01'});

	RETURN @Result

END

